package com.ygxsk.carhome.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * Autuhor:Administrator
 * Time:2023-03-27  14:07
 * Description: this is
 */
@SuppressLint("AppCompatCustomView")
public class ClearEditTextTouch extends EditText {

    //    private Drawable mClearDrawable;
//    private boolean hasFocus;

    public ClearEditTextTouch(Context context) {
        this(context, null);
    }

    public ClearEditTextTouch(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClearEditTextTouch(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {

//        // getCompoundDrawables() Returns drawables for the left(0), top(1), right(2) and bottom(3)
//        mClearDrawable = getCompoundDrawables()[2]; // 获取drawableRight
//        if (mClearDrawable == null) {
//            // 如果为空，即没有设置drawableRight，则使用R.mipmap.close这张图片
//            //mClearDrawable = getResources().getDrawable(R.mipmap.close);
//            mClearDrawable = getResources().getDrawable(R.drawable.ic_edit_text_clear);
//        }
//        mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());

//        setOnFocusChangeListener(this);
//        addTextChangedListener(this);
        // 默认隐藏图标
//        setDrawableVisible(false);

        //直接从父类过滤表情符号和空格输入。
        setFilters(new InputFilter[]{new EmojiFilter()});
    }


//    @Override
//    public void onFocusChange(View v, boolean hasFocus) {
//
//    }

    /**
     * 我们无法直接给EditText设置点击事件，只能通过按下的位置来模拟clear点击事件
     * 当我们按下的位置在图标包括图标到控件右边的间距范围内均算有效
     */
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        if (event.getAction() == MotionEvent.ACTION_UP) {
//            if (getCompoundDrawables()[2] != null) {
//                int start = getWidth() - getTotalPaddingRight() + getPaddingRight(); // 起始位置
//                int end = getWidth(); // 结束位置
//                boolean available = (event.getX() > start) && (event.getX() < end);
//                if (available) {
//                    this.setText("");
//                }
//            }
//        }
//        return super.onTouchEvent(event);
//    }

//    @Override
//    public void onFocusChange(View v, boolean hasFocus) {
//
////        this.hasFocus = hasFocus;
////        setDrawableVisible(hasFocus && getText().length() > 0); //有焦点且有文字时显示图标
//
//    }

//    @Override
//    public void onTextChanged(CharSequence s, int start, int count, int after) {
////        if (hasFocus) {
////            setDrawableVisible(s.length() > 0);
////        }
//    }

//    @Override
//    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//    }
//
//    @Override
//    public void afterTextChanged(Editable s) {
//    }

//    protected void setDrawableVisible(boolean visible) {
//        Drawable right = visible ? mClearDrawable : null;
//        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
//    }

    /**
     * 禁止EditText输入空格
     */
    /*public void setEditTextInhibitInputSpace() {

        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ")) return "";
                else return null;
            }
        };

        this.setFilters(new InputFilter[]{filter});

    }*/

    /**
     * 禁止EditText输入特殊字符，但是2019年12月08日测试发现但是可以输入横线和下划线。
     */
    /*public void setEditTextInhibitInputSpeChat() {

        InputFilter filter = new InputFilter() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());

                if (matcher.find())
                    return "";
                else return null;
            }
        };

        this.setFilters(new InputFilter[]{filter});
    }*/
    //原文链接：https://blog.csdn.net/a1533588867/article/details/53035475
}
